package com.vincent.filepicker.adapter;

import com.vincent.filepicker.ResourceTable;
import com.vincent.filepicker.ToastUtil;
import com.vincent.filepicker.Util;
import com.vincent.filepicker.filter.entity.NormalFile;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * Created by Vincent Woo
 * Date: 2016/10/26
 * Time: 10:23
 */

public class NormalFilePickAdapter extends BaseAdapter<NormalFile> {
    private int mMaxNumber;
    private int mCurrentNumber = 0;

    public NormalFilePickAdapter(Context ctx, int max) {
        this(ctx, new ArrayList<NormalFile>(), max);
    }

    public NormalFilePickAdapter(Context ctx, ArrayList<NormalFile> list, int max) {
        super(ctx, list);
        mMaxNumber = max;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    NormalFilePickViewHolder viewHolder = null;

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        if (component == null) {
            viewHolder = new NormalFilePickViewHolder();
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_vw_layout_item_normal_file_pick, null, false);
            viewHolder.mIvIcon = (Image) component.findComponentById(ResourceTable.Id_ic_file);
            viewHolder.mTvTitle = (Text) component.findComponentById(ResourceTable.Id_tv_file_title);
            viewHolder.mCbx = (Image) component.findComponentById(ResourceTable.Id_cbx);
            component.setTag(viewHolder);
        } else {
            viewHolder = (NormalFilePickViewHolder) component.getTag();
        }

        final NormalFile file = mList.get(position);

        viewHolder.mTvTitle.setText(Util.extractFileNameWithSuffix(file.getPath()));

        if (file.isSelected()) {
            viewHolder.mCbx.setSelected(true);
        } else {
            viewHolder.mCbx.setSelected(false);
        }

        if (file.getPath().endsWith("xls") || file.getPath().endsWith("xlsx")) {
            viewHolder.mIvIcon.setPixelMap(ResourceTable.Media_vw_ic_excel);
        } else if (file.getPath().endsWith("doc") || file.getPath().endsWith("docx")) {
            viewHolder.mIvIcon.setPixelMap(ResourceTable.Media_vw_ic_word);
        } else if (file.getPath().endsWith("ppt") || file.getPath().endsWith("pptx")) {
            viewHolder.mIvIcon.setPixelMap(ResourceTable.Media_vw_ic_ppt);
        } else if (file.getPath().endsWith("pdf")) {
            viewHolder.mIvIcon.setPixelMap(ResourceTable.Media_vw_ic_pdf);
        } else if (file.getPath().endsWith("txt")) {
            viewHolder.mIvIcon.setPixelMap(ResourceTable.Media_vw_ic_txt);
        } else {
            viewHolder.mIvIcon.setPixelMap(ResourceTable.Media_vw_ic_file);
        }

        viewHolder.mCbx.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                if (!v.isSelected() && isUpToMax()) {
                    ToastUtil.getInstance(mContext).showToast(ResourceTable.String_vw_up_to_max);
                    return;
                }

                if (v.isSelected()) {
                    viewHolder.mCbx.setSelected(false);
                    mCurrentNumber--;
                } else {
                    viewHolder.mCbx.setSelected(true);
                    mCurrentNumber++;
                }

                mList.get(position).setSelected(viewHolder.mCbx.isSelected());

                if (mListener != null) {
                    mListener.OnSelectStateChanged(viewHolder.mCbx.isSelected(), mList.get(position));
                }
            }
        });


        return component;
    }

    class NormalFilePickViewHolder {
        private Image mIvIcon;
        private Text mTvTitle;
        private Image mCbx;
    }

    private boolean isUpToMax() {
        return mCurrentNumber >= mMaxNumber;
    }

}
